// Proxy元编程 -- 代理，拦截
// 两个参数，参数一拦截的对象，参数二：处理方法handler
// 必须针对Proxy实例而不是目标对象

var obj = new Proxy({}, {
  get: function (target, key, receiver) {
    return Reflect.get(target, key, receiver)
  },
  set(target, key, value, receiver) {
    return Reflect.set(target, key, value, receiver)
  }
})


// 如果handler没有设置任何拦截，哪就等同于直接通向原对象

let target = {}
let handler = {}

let proxy = new Proxy(target, handler)

proxy.a = 'b'
console.log(target.a);

// proxy对象也可以作为其他对象的原型对象


// 实现A2 = A0+A1

let A0, A1, A2;
let A2Dom = document.querySelector('#A2')



A0 = new Proxy({ value: null }, {
  set(target, key, value, receiver) {
    console.log('拦截A0的set通知它的订阅者');
    update()// 拦截A0的set通知它的订阅者
    return Reflect.set(target, key, value, receiver)
  }
})

A1 = new Proxy({ value: null }, {
  set(target, key, value, receiver) {
    console.log('拦截A1的set通知它的订阅者');
    update()//拦截A1的set通知它的订阅者
    return Reflect.set(target, key, value, receiver)
  }
})



function A0Change(e) {
  A0.value = Number(e.target.value)
  update()
}

function A1Change(e) {
  A1.value = Number(e.target.value)
  update()
}


//作用update是A0,A1的订阅者
function update() {
  A2 = A0.value + A1.value
  A2Dom.innerText = A2
}




